//
//  HWOAuthViewController.m
//  WeiBTest
//
//  Created by Zc_zhou on 15/9/11.
//  Copyright (c) 2015年 Apple. All rights reserved.
//
//
//App Key：3669386018
//App Secret：e617e3f2e234b09544734081a87339b6

#import "HWOAuthViewController.h"
#import "OAuthModel.h"
#import "MainViewController.h"
#import "NewFeatureViewController.h"
@interface HWOAuthViewController ()<UIWebViewDelegate>

@end

@implementation HWOAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    webView.delegate = self;
    [self.view addSubview:webView];
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3669386018&redirect_uri=http://www.baodu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];

}
#pragma mark - webView代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
        HWLog(@"----webViewDidFinishLoad");
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
       HWLog(@"----webViewDidStartLoad");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"code="];
    if (range.length !=0) {
        int fromIndex = (int)(range.location +range.length);
        NSString *code = [url substringFromIndex:fromIndex];
        [self accessTokenWithCode:code];
        return NO;//这样才不会返回到百度这个网址
    }
    return YES;
}
- (void)accessTokenWithCode:(NSString*)code
{
    /*
     URL：https://api.weibo.com/oauth2/access_token
     
     请求参数：
     client_id：申请应用时分配的AppKey
     client_secret：申请应用时分配的AppSecret
     grant_type：使用authorization_code
     redirect_uri：授权成功后的回调地址
     code：授权成功后返回的code
     */
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//    mgr.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"client_id"] = @"3669386018";
    parameters[@"client_secret"] = @"e617e3f2e234b09544734081a87339b6";
    parameters[@"grant_type"] = @"authorization_code";
    parameters[@"redirect_uri"] = @"http://www.baodu.com";
    parameters[@"code"] = code;

    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
       
        
        OAuthModel *account = [OAuthModel accountWithDict:responseObject];
        
        [OAuthTool saveAccount:account];
        
        [self.view.window switchRootViewControl];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        HWLog(@"请求失败－%@",error);
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
